﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LCWMS
{
    public partial class w_so019_add : Form
    {
        public static string consignor, so_no, wave_no;
        public static int i_dist = 0;
        int i_rows_count = 0;
        private string i_sql_so_pack_header;
        private string i_sql_so_pack_header_s;
        private DataSet i_ds_so_pack_header = new DataSet();
        private DataSet i_ds_so_pack_header_s = new DataSet();
        string box_no_s;
        private int i_close = 0;
        Rectangle _Rectangle;
        ComboBox cmb_address = new ComboBox();
        public w_so019_add()
        {
            InitializeComponent();
            dgv_so_pack_header_Zi.AutoGenerateColumns = false;

            cmb_address.Visible = false;  //先不让它显示            
            //cmb_address.Format = ComboBoxFormat.Custom;  //设置日期格式为2010-08-05  
            dgv_so_pack_header_Zi.Controls.Add(cmb_address);
            //cmb_address.MouseClick += new EventHandler(cmb_address_Change); //为时间控件加入事件dtp_TextChange
            cmb_address.SelectedIndexChanged += new EventHandler(cmb_address_SelectedIndexChanged);
            cmb_address.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        void cmb_address_SelectedIndexChanged(object sender, EventArgs e)
        {
            dgv_so_pack_header_Zi.CurrentRow.Cells["address"].Value = cmb_address.Text.ToString();  //时间控件选择时间时，就把时间赋给所在的单元格
            //cmb_address.Visible = false;
        }


        #region 自定义函数
        private DataSet f_get_so_pack_header(string consignor, string so_no)
        {

            i_sql_so_pack_header_s = "select b.inv_org,b.consignor,b.so_no,b.box_no,b.spec,b.box_size,b.net_weight,b.send_no,b.carrier,a.address,b.carrier_postalcode box_number,ltrim(rtrim(b.carrier_phone)) repack_type,b.ship_no,b.fly_no,b.vehicle_no,b.memo from " + main.g_space + ".so_ship_header a, " + main.g_space + ".so_pack_header b" +
                               " where a.inv_org=b.inv_org and a.consignor = b.consignor and a.so_no = b.so_no and b.status ='0' and b.inv_org='" + main.g_org + "' and b.consignor='" + consignor + "' and b.so_no ='" + so_no + "'";
                i_sql_so_pack_header = "select consignor,so_no,box_no,spec,box_size,net_weight,send_no,carrier,address,carrier_postalcode,carrier_phone,ship_no,fly_no,vehicle_no,memo,inv_org from " + main.g_space + ".so_pack_header " +
                               "where status ='0' and inv_org='" + main.g_org + "' and consignor='" + consignor + "' and so_no = '" + so_no + "'";
            try
            {
                if (main.dbtype == "SQL")
                {
                    i_ds_so_pack_header = main.dbservice.Sql_exec_selectsql(i_sql_so_pack_header);
                    i_ds_so_pack_header_s = main.dbservice.Sql_exec_selectsql(i_sql_so_pack_header);
                }
                else if (main.dbtype == "ORACLE")
                {
                    i_ds_so_pack_header = main.dbservice.oracle_exec_selectsql(i_sql_so_pack_header);
                    i_ds_so_pack_header_s = main.dbservice.oracle_exec_selectsql(i_sql_so_pack_header);
                }
                i_rows_count = i_ds_so_pack_header.Tables[0].Rows.Count;
                return i_ds_so_pack_header;

            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get bill's Information", main.g_info_dataset) + "!" + ee.ToString());
                return null;
            }
        }

        /// <summary>
        /// 得到客户信息
        /// </summary>
        ///// <returns></returns>
        //private DataSet f_get_customer()
        //{
            //string s_sql = "select consignor_name carrier from " + main.g_space + ".mst_customer where consignor_flag='1' and address4='1' and inv_org='" + main.g_org + "'";
            //DataSet ds = new DataSet();
            //try
            //{
            //    if (main.dbtype == "SQL")
            //    {
            //        ds = main.dbservice.Sql_exec_selectsql(s_sql);
            //    }
            //    else if (main.dbtype == "ORACLE")
            //    {
            //        ds = main.dbservice.oracle_exec_selectsql(s_sql);
            //    }
            //    return ds;

            //}
            //catch (System.Exception ee)
            //{
            //    MessageBox.Show(main.g_class.comm_getchangestring("Fail to get bill's Information", main.g_info_dataset) + "!" + ee.ToString());
            //    return null;
            //}

        //}
        #endregion

        private void w_so19_so_pack_header_Load(object sender, EventArgs e)
        {
            DataSet ds_temp_2 = new DataSet();
            ds_temp_2.Tables.Add("0");
            ds_temp_2.Tables[0].Columns.Add("carrier_phone");
            ds_temp_2.Tables[0].Columns.Add("repack_type");
            DataRow drnew;
            drnew = ds_temp_2.Tables[0].NewRow();
            drnew["carrier_phone"] = "0";
            drnew["repack_type"] = "否";
            ds_temp_2.Tables[0].Rows.Add(drnew);

            DataRow drnew2;
            drnew2 = ds_temp_2.Tables[0].NewRow();
            drnew2["carrier_phone"] = "1";
            drnew2["repack_type"] = "是";
            ds_temp_2.Tables[0].Rows.Add(drnew2);

            DataGridViewComboBoxColumn dgvComboBoxColumn = dgv_so_pack_header_Zi.Columns["repack_type"] as DataGridViewComboBoxColumn;
            //dgvComboBoxColumn.DataPropertyName = "transaction_type";               
            dgvComboBoxColumn.DataSource = ds_temp_2.Tables[0].Copy();//必须在设置dataGridView1的DataSource的属性前设置
            dgvComboBoxColumn.DisplayMember = "repack_type";
            dgvComboBoxColumn.ValueMember = "carrier_phone";

            //规格
            DataSet ds_temp_3 = new DataSet();
            ds_temp_3.Tables.Add("0");
            ds_temp_3.Tables[0].Columns.Add("spec_code");
            ds_temp_3.Tables[0].Columns.Add("spec");
            DataRow drnew3;
            drnew3 = ds_temp_3.Tables[0].NewRow();
            drnew3["spec_code"] = "纸箱";
            drnew3["spec"] = "纸箱";
            ds_temp_3.Tables[0].Rows.Add(drnew3);

            DataRow drnew4;
            drnew4 = ds_temp_3.Tables[0].NewRow();
            drnew4["spec_code"] = "木箱";
            drnew4["spec"] = "木箱";
            ds_temp_3.Tables[0].Rows.Add(drnew4);

            DataRow drnew5;
            drnew5 = ds_temp_3.Tables[0].NewRow();
            drnew5["spec_code"] = "小桶油";
            drnew5["spec"] = "小桶油";
            ds_temp_3.Tables[0].Rows.Add(drnew5);

            DataRow drnew6;
            drnew6 = ds_temp_3.Tables[0].NewRow();
            drnew6["spec_code"] = "大桶油";
            drnew6["spec"] = "大桶油";
            ds_temp_3.Tables[0].Rows.Add(drnew6);

            DataRow drnew7;
            drnew7 = ds_temp_3.Tables[0].NewRow();
            drnew7["spec_code"] = "托盘";
            drnew7["spec"] = "托盘";
            ds_temp_3.Tables[0].Rows.Add(drnew7);

            DataGridViewComboBoxColumn dgvComboBoxColumn2 = dgv_so_pack_header_Zi.Columns["spec"] as DataGridViewComboBoxColumn;
            //dgvComboBoxColumn.DataPropertyName = "transaction_type";               
            dgvComboBoxColumn2.DataSource = ds_temp_3.Tables[0].Copy();//必须在设置dataGridView1的DataSource的属性前设置
            dgvComboBoxColumn2.DisplayMember = "spec";
            dgvComboBoxColumn2.ValueMember = "spec_code";


            if (i_close == 0)
            {
                i_close = 1;
                DataSet ds = new DataSet();
                ////绑定客户
                //try
                //{

                //    ds = f_get_customer();
                //    DataGridViewComboBoxColumn dgvComboBoxColumn = dgv_so_pack_header_Zi.Columns[8] as DataGridViewComboBoxColumn;
                //    dgvComboBoxColumn.DataPropertyName = "carrier";
                //    dgvComboBoxColumn.DataSource = ds.Tables[0].DefaultView;//必须在设置dataGridView1的DataSource的属性前设置
                //    dgvComboBoxColumn.DisplayMember = "carrier";
                //    dgvComboBoxColumn.ValueMember = "carrier";
                //}
                //catch (System.Exception ee)
                //{
                //    MessageBox.Show("收货类新提取失败!" + ee.ToString());
                //    return;
                //}


                //语言转换
                main.g_class.comm_changeControl(this, main.g_win_dataset);
                ////初始化订单信息
                //try
                //{
                //    f_ds = main.g_class.comm_get_dictionary("asn_type");
                //    //进行语言转换
                //    int i_ds;
                //    i_ds = f_ds.Tables[0].Rows.Count;
                //    if (i_ds > 0)
                //    {
                //        for (int i = 0; i < i_ds; i++)
                //        {
                //            f_ds.Tables[0].Rows[i]["asn_type"] = main.g_class.comm_getchangestring(f_ds.Tables[0].Rows[i]["asn_type"].ToString().Trim(), main.g_info_dataset);
                //        }
                //    }
                //    DataSet newf_ds = f_ds.Copy();
                //    DataRow drnew;
                //    drnew = f_ds.Tables[0].NewRow();
                //    drnew["asn_type"] = "";
                //    f_ds.Tables[0].Rows.Add(drnew);
                //    cbx_asntype.ValueMember = "asn_type";
                //    cbx_asntype.DisplayMember = "asn_type";

                //    cbx_asntype.DataSource = f_ds.Tables[0];

                //    main.g_class.comm_changeCbxContent(cbx_pallet, main.g_info_dataset, '1');

                //}
                //catch (System.Exception ee)
                //{
                //    MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize Information of bill's type", main.g_info_dataset) + "!" + ee.ToString());
                //    return;
                //}


                //绑定dgv_so_pach_header
                ds = f_get_so_pack_header(consignor, so_no);
                dgv_so_pack_header_Zi.DataSource = ds.Tables[0];

                //新增
                if (i_dist == 1)
                {
                    //新增一行
                    ((DataTable)dgv_so_pack_header_Zi.DataSource).Rows.Add();
                    //带出客户和单号
                    dgv_so_pack_header_Zi.Rows[dgv_so_pack_header_Zi.Rows.Count - 1].Cells["so_pack_header_consignor"].Value = consignor;
                    dgv_so_pack_header_Zi.Rows[dgv_so_pack_header_Zi.Rows.Count - 1].Cells["so_pack_header_so_no"].Value = so_no;
                    dgv_so_pack_header_Zi.Rows[dgv_so_pack_header_Zi.Rows.Count - 1].Cells["inv_org"].Value = main.g_org;
                    dgv_so_pack_header_Zi.Rows[dgv_so_pack_header_Zi.Rows.Count - 1].Cells["repack_type"].Value = '0';
                    if (dgv_so_pack_header_Zi.Rows.Count > 1)
                    { 
                        dgv_so_pack_header_Zi.Rows[dgv_so_pack_header_Zi.Rows.Count - 1].Cells["spec"].Value =dgv_so_pack_header_Zi.Rows[0].Cells["spec"].Value;
                        dgv_so_pack_header_Zi.Rows[dgv_so_pack_header_Zi.Rows.Count - 1].Cells["send_no"].Value = dgv_so_pack_header_Zi.Rows[0].Cells["send_no"].Value;
                    }
                    dgv_so_pack_header_Zi.Rows[dgv_so_pack_header_Zi.Rows.Count - 1].Selected = true;

                    //光标显示在新增箱子列
                    int rows = dgv_so_pack_header_Zi.Rows.Count - 1;
                    dgv_so_pack_header_Zi.CurrentCell = dgv_so_pack_header_Zi["so_pack_header_box_no", rows];
                    //dgv_so_pack_header_Zi.BeginEdit(true);
                }
                else if (i_dist == 2)
                {
                    i_dist = 2;
                }
            }
            else
            {
                this.Close();
            }
        }

        private void dgv_so_pack_header_CurrentCellChanged(object sender, EventArgs e)
        {
            //如果有数据
            if (dgv_so_pack_header_Zi.CurrentRow != null)
            {
                //编辑控制
                //  
                if (i_dist == 1)
                {
                    //如果是新增行
                    if (dgv_so_pack_header_Zi.CurrentRow.Index > i_rows_count - 1)
                    {
                        dgv_so_pack_header_Zi.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
                    }
                    else
                    {
                        dgv_so_pack_header_Zi.EditMode = DataGridViewEditMode.EditProgrammatically;
                    }
                }
                //修改
                else if (i_dist == 2)
                {
                    dgv_so_pack_header_Zi.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
                }
                else
                {
                    //设置不可编辑
                    dgv_so_pack_header_Zi.EditMode = DataGridViewEditMode.EditProgrammatically;
                }
            }
        }

        private DataSet ds_temp = new DataSet();
        private DataSet ds_temp_2 = new DataSet();
        //保存
        private void btn_subsave_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dgv_so_pack_header_Zi.Rows.Count; i++)
            {
                //箱号不能为空
                if (string.IsNullOrEmpty(dgv_so_pack_header_Zi.Rows[i].Cells["so_pack_header_box_no"].Value.ToString().Trim()))
                {
                    MessageBox.Show(main.g_class.comm_getchangestring("box_no not null", main.g_info_dataset) + "!");
                    dgv_so_pack_header_Zi.CurrentCell = dgv_so_pack_header_Zi["so_pack_header_box_no", i];
                    dgv_so_pack_header_Zi.BeginEdit(true);
                    return;
                }

            }

            //判断是新增还是修改
            if (i_dist == 1)
            {//如果是新增
                dgv_so_pack_header_Zi.EndEdit();
                int i = dgv_so_pack_header_Zi.Rows.Count - 1;


                if (dgv_so_pack_header_Zi.Rows[i].Cells["send_no"].Value.ToString().Trim() != "")
                {
                    string str_sql = "select ltrim(rtrim(case use_flag when '1' then content1 when '2' then content2 when '3' then convert(char,number1) else convert(char,number2) end)) content1 from " + main.g_space + ".sys_dictionary  where inv_org='" + main.g_org +
                        "' and dictionary_name ='spec'" +
                        " and dictionary_code = '" + box_no_s + "'";
                    string str_sql2 = "select count(*) count from " + main.g_space + ".so_pack_header  where status ='0' and so_no ='" + dgv_so_pack_header_Zi.Rows[i].Cells["so_pack_header_so_no"].Value.ToString().Trim() + "' and send_no ='" + dgv_so_pack_header_Zi.Rows[i].Cells["send_no"].Value.ToString().Trim() + "'";

                    try
                    {
                        if (main.dbtype == "SQL")
                        {
                            ds_temp = main.dbservice.Sql_exec_selectsql(str_sql);
                            ds_temp_2 = main.dbservice.Sql_exec_selectsql(str_sql2);
                        }
                        else if (main.dbtype == "ORACLE")
                        {
                            ds_temp = main.dbservice.oracle_exec_selectsql(str_sql);
                            ds_temp_2 = main.dbservice.oracle_exec_selectsql(str_sql2);
                        }
                        if (ds_temp.Tables[0].Rows.Count < 1)
                        {
                            MessageBox.Show(main.g_class.comm_getchangestring("箱号不存在", main.g_info_dataset) + "!");
                            int rows = this.dgv_so_pack_header_Zi.CurrentRow.Index;
                            dgv_so_pack_header_Zi.CurrentCell = dgv_so_pack_header_Zi["so_pack_header_box_no", rows];
                            dgv_so_pack_header_Zi.BeginEdit(true);
                            return;
                        }
                        if (dgv_so_pack_header_Zi.Rows[i].Cells["box_size"].Value.ToString().Trim() == "")
                            dgv_so_pack_header_Zi.Rows[i].Cells["box_size"].Value = ds_temp.Tables[0].Rows[0]["content1"].ToString().Trim();
                        int count = Convert.ToInt32(ds_temp_2.Tables[0].Rows[0]["count"].ToString().Trim()) + 1;
                        string no;
                        if (count < 10)
                        {
                            no = "00" + count.ToString();
                        }
                        else if (count < 100)
                        {
                            no = "00" + count.ToString();
                        }
                        else
                        {
                            no = count.ToString();
                        }

                        dgv_so_pack_header_Zi.Rows[i].Cells["so_pack_header_box_no"].Value = dgv_so_pack_header_Zi.Rows[i].Cells["send_no"].Value.ToString().Trim() + "-" + no;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                        return;
                    }
                    int length = dgv_so_pack_header_Zi.Rows.Count - 1;
                    string net_weight;
                    if (dgv_so_pack_header_Zi.Rows[length].Cells["net_weight"].Value.ToString().Trim() == "")
                        net_weight = "null";
                    else
                        net_weight = dgv_so_pack_header_Zi.Rows[length].Cells["net_weight"].Value.ToString().Trim();

                    string address;
                    if (dgv_so_pack_header_Zi.Rows[length].Cells["address"].Value.ToString().Trim() == "")
                        address = "";
                    else
                        address = dgv_so_pack_header_Zi.Rows[length].Cells["address"].Value.ToString().Trim().Substring(4, dgv_so_pack_header_Zi.Rows[length].Cells["address"].Value.ToString().Trim().Length - 4);


                    string box_number;
                    if (dgv_so_pack_header_Zi.Rows[length].Cells["box_number"].Value.ToString().Trim() == "")
                        box_number = "";
                    else
                        box_number = dgv_so_pack_header_Zi.Rows[length].Cells["box_number"].Value.ToString().Trim();

                    string repack_type;
                    if (dgv_so_pack_header_Zi.Rows[length].Cells["repack_type"].Value.ToString().Trim() == "")
                        repack_type = "";
                    else
                        repack_type = dgv_so_pack_header_Zi.Rows[length].Cells["repack_type"].Value.ToString();

                    try
                    {
                        if (main.dbtype == "SQL")
                        {
                            string str_sql3 = "insert into " + main.g_space + ".so_pack_header (inv_org,consignor,so_no,box_no,spec,box_size,net_weight,send_no,carrier,address,carrier_postalcode,carrier_phone,ship_no,fly_no,vehicle_no,status,memo,main_dist,made_time,maker,standby_1) values ( '" +
                                        main.g_org + "','" + dgv_so_pack_header_Zi.Rows[length].Cells["so_pack_header_consignor"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["so_pack_header_so_no"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["so_pack_header_box_no"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["spec"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["box_size"].Value.ToString().Trim() + "'," +
                                        net_weight + ",'" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["send_no"].Value.ToString().Trim() + "','"+
                                        dgv_so_pack_header_Zi.Rows[length].Cells["carrier"].Value.ToString().Trim() + "','" +
                                         address + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["box_number"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["repack_type"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["ship_no"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["fly_no"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["vehicle_no"].Value.ToString().Trim() + "'" +
                                        ",'0','" +dgv_so_pack_header_Zi.Rows[length].Cells["memo"].Value.ToString().Trim() + "','A',getdate(),'" + main.g_user + "','0')";
                            //string str_sql4 = "update " + main.g_space + ".so_ship_header set  main_dist ='C',update_time = getdate(),updater='" + main.g_username + "', address ='" + dgv_so_pack_header_Zi.Rows[length].Cells["address"].Value.ToString().Trim().Substring(4, dgv_so_pack_header_Zi.Rows[length].Cells["address"].Value.ToString().Trim().Length-4) + "' where inv_org='" + main.g_org +
                            //                "' and consignor ='" + dgv_so_pack_header_Zi.Rows[length].Cells["so_pack_header_consignor"].Value.ToString().Trim() + "' and so_no ='" + dgv_so_pack_header_Zi.Rows[length].Cells["so_pack_header_so_no"].Value.ToString().Trim() + "'";
                            //main.dbservice.Sql_exec_commandsql(str_sql4);
                            main.dbservice.Sql_exec_commandsql(str_sql3);
                        }
                        else if (main.dbtype == "ORACLE")
                        {
                            string str_sql3 = "insert into " + main.g_space + ".so_pack_header (inv_org,consignor,so_no,box_no,spec,box_size,net_weight,send_no,carrier,address,carrier_postalcode,carrier_phone,ship_no,fly_no,vehicle_no,status,memo,main_dist,made_time,maker,standby_1) values ( '" +
                                        main.g_org + "','" + dgv_so_pack_header_Zi.Rows[length].Cells["so_pack_header_consignor"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["so_pack_header_so_no"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["so_pack_header_box_no"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["spec"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["box_size"].Value.ToString().Trim() + "'," +
                                        net_weight + ",'" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["send_no"].Value.ToString().Trim() + "','"+
                                        dgv_so_pack_header_Zi.Rows[length].Cells["carrier"].Value.ToString().Trim() + "','" +
                                        address + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["box_number"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["repack_type"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["ship_no"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["fly_no"].Value.ToString().Trim() + "','" +
                                        dgv_so_pack_header_Zi.Rows[length].Cells["vehicle_no"].Value.ToString().Trim() + "'" +
                                        ",'0','" + dgv_so_pack_header_Zi.Rows[length].Cells["memo"].Value.ToString().Trim() + "','A',sysdate,'" + main.g_user + "','0')";
                            //string str_sql4 = "update " + main.g_space + ".so_ship_header set  main_dist ='C',update_time =sysdate,updater='" + main.g_username + "', address ='" + dgv_so_pack_header_Zi.Rows[length].Cells["address"].Value.ToString().Trim().Substring(4, dgv_so_pack_header_Zi.Rows[length].Cells["address"].Value.ToString().Trim().Length-4) + "' where inv_org='" + main.g_org +
                            //                "' and consignor ='" + dgv_so_pack_header_Zi.Rows[length].Cells["so_pack_header_consignor"].Value.ToString().Trim() + "' and so_no ='" + dgv_so_pack_header_Zi.Rows[length].Cells["so_pack_header_so_no"].Value.ToString().Trim() + "'";
                            main.dbservice.oracle_exec_commandsql(str_sql3);
                            //main.dbservice.oracle_exec_commandsql(str_sql4);
                        }
                    }
                    catch (Exception ee)
                    {
                        MessageBox.Show(ee.Message.ToString());
                        return;
                    }

                }
                else
                {
                    MessageBox.Show(main.g_class.comm_getchangestring("请输入运单号", main.g_info_dataset) + "!");
                    int rows = this.dgv_so_pack_header_Zi.CurrentRow.Index;
                    dgv_so_pack_header_Zi.CurrentCell = dgv_so_pack_header_Zi["send_no", rows];
                    dgv_so_pack_header_Zi.BeginEdit(true);
                    return;
                }

            }
            else if (i_dist == 2)
            {
                //i_sql_so_pack_header = "select * from " + main.g_space + ".so_pack_header " +
                //           "where inv_org='" + main.g_org + "' and consignor='" + consignor + "' and box_bo = '"+
                //try
                //{
                //    if (main.dbtype == "SQL")
                //    {
                //        main.dbservice.Sql_refresh_db(i_ds_so_pack_header, i_sql_so_pack_header);
                //    }
                //    else if (main.dbtype == "ORACLE")
                //    {
                //        main.dbservice.oracle_refresh_db(i_ds_so_pack_header, i_sql_so_pack_header);
                //    }
                //    MessageBox.Show(main.g_class.comm_getchangestring("saved", main.g_info_dataset) + "!");
                //}
                //catch (Exception ee)
                //{
                //    MessageBox.Show(main.g_class.comm_getchangestring("Fail to save box", main.g_info_dataset) + "!" + ee.ToString());
                //    return;
                //}
                dgv_so_pack_header_Zi.EndEdit();
                for (int i = 0; i < i_ds_so_pack_header_s.Tables[0].Rows.Count; i++)
                {
                    string net_weight;
                    if (dgv_so_pack_header_Zi.Rows[i].Cells["net_weight"].Value.ToString().Trim() == "")
                        net_weight = "null";
                    else
                        net_weight = dgv_so_pack_header_Zi.Rows[i].Cells["net_weight"].Value.ToString().Trim();

                    string i_address = "";
                    if (i_ds_so_pack_header_s.Tables[0].Rows[i]["address"].ToString().Trim() != i_ds_so_pack_header.Tables[0].Rows[i]["address"].ToString().Trim())
                    {

                        if (dgv_so_pack_header_Zi.Rows[i].Cells["address"].Value.ToString().Trim() == "")
                            i_address = "";
                        else
                            i_address = dgv_so_pack_header_Zi.Rows[i].Cells["address"].Value.ToString().Trim().Substring(4, dgv_so_pack_header_Zi.Rows[i].Cells["address"].Value.ToString().Trim().Length - 4);
                    }
                    else
                    {
                        i_address = dgv_so_pack_header_Zi.Rows[i].Cells["address"].Value.ToString().Trim();
                    }

                    string box_number;
                    if (dgv_so_pack_header_Zi.Rows[i].Cells["box_number"].Value.ToString().Trim() == "")
                        box_number = "";
                    else
                        box_number = dgv_so_pack_header_Zi.Rows[i].Cells["box_number"].Value.ToString().Trim();

                    string repack_type;
                    if (dgv_so_pack_header_Zi.Rows[i].Cells["repack_type"].Value.ToString().Trim() == "")
                        repack_type = "";
                    else
                        repack_type = dgv_so_pack_header_Zi.Rows[i].Cells["repack_type"].Value.ToString();

                        try
                        {
                            if (main.dbtype == "SQL")
                            {
                                i_sql_so_pack_header = " update " + main.g_space + ".so_pack_header set carrier ='" + dgv_so_pack_header_Zi.Rows[i].Cells["carrier"].Value.ToString().Trim() + "', main_dist ='C',update_time =getdate(),updater='" + main.g_user + "', box_no='" + dgv_so_pack_header_Zi.Rows[i].Cells["so_pack_header_box_no"].Value.ToString().Trim() +
                                            "' ,spec ='" + dgv_so_pack_header_Zi.Rows[i].Cells["spec"].Value.ToString().Trim() +
                                            "',send_no ='" + dgv_so_pack_header_Zi.Rows[i].Cells["send_no"].Value.ToString().Trim() +
                                            "', address ='" + i_address +
                                            "',net_weight = "+net_weight+
                                            ",box_size ='" + dgv_so_pack_header_Zi.Rows[i].Cells["box_size"].Value.ToString().Trim() +
                                            "', carrier_postalcode='"+dgv_so_pack_header_Zi.Rows[i].Cells["box_number"].Value.ToString().Trim()+
                                           "', carrier_phone='" + dgv_so_pack_header_Zi.Rows[i].Cells["repack_type"].Value.ToString().Trim() +
                                           "', memo='" + dgv_so_pack_header_Zi.Rows[i].Cells["memo"].Value.ToString().Trim() +
                                            "' where status ='0' and box_no = '" + i_ds_so_pack_header_s.Tables[0].Rows[i]["box_no"].ToString().Trim() + "'";
                                i_sql_so_pack_header += " and so_no in (select so_no from " + main.g_space + ".so_ship_wavedetail where inv_org='" + main.g_org + "' and consignor='" + dgv_so_pack_header_Zi.Rows[i].Cells["so_pack_header_consignor"].Value.ToString().Trim() + "' and wave_no='" + wave_no + "') ";
                                string i_sql_so_pack_detail = " update " + main.g_space + ".so_pack_detail set  main_dist ='C',update_time =getdate(),updater='" + main.g_user + "', box_no='" + dgv_so_pack_header_Zi.Rows[i].Cells["so_pack_header_box_no"].Value.ToString().Trim() +
                                             "' where box_no = '" + i_ds_so_pack_header_s.Tables[0].Rows[i]["box_no"].ToString().Trim() + "'";
                                i_sql_so_pack_detail += " and so_no in (select so_no from " + main.g_space + ".so_ship_wavedetail where inv_org='" + main.g_org + "' and consignor='" + dgv_so_pack_header_Zi.Rows[i].Cells["so_pack_header_consignor"].Value.ToString().Trim() + "' and wave_no='" + wave_no + "') ";
                                //string str_sql = "update " + main.g_space + ".so_ship_header set  main_dist ='C',update_time =getdate(),updater='" + main.g_username + "', address ='" + dgv_so_pack_header_Zi.Rows[i].Cells["address"].Value.ToString().Trim().Substring(4, dgv_so_pack_header_Zi.Rows[i].Cells["address"].Value.ToString().Trim().Length-4) + "' where inv_org='" + main.g_org +
                                //                "' and consignor ='" + dgv_so_pack_header_Zi.Rows[i].Cells["so_pack_header_consignor"].Value.ToString().Trim() + "' and so_no ='" + dgv_so_pack_header_Zi.Rows[i].Cells["so_pack_header_so_no"].Value.ToString().Trim() + "'";

                                main.dbservice.Sql_exec_commandsql(i_sql_so_pack_header);
                                main.dbservice.Sql_exec_commandsql(i_sql_so_pack_detail);
                                //main.dbservice.Sql_exec_commandsql(str_sql);
                            }
                            else if (main.dbtype == "ORACLE")
                            {
                                i_sql_so_pack_header = " update " + main.g_space + ".so_pack_header set carrier ='" + dgv_so_pack_header_Zi.Rows[i].Cells["carrier"].Value.ToString().Trim() + "' main_dist ='C',update_time =sysdate,updater='" + main.g_user + "', box_no='" + dgv_so_pack_header_Zi.Rows[i].Cells["so_pack_header_box_no"].Value.ToString().Trim() +
                                            "' ,spec ='" + dgv_so_pack_header_Zi.Rows[i].Cells["spec"].Value.ToString().Trim() +
                                            "',send_no ='" + dgv_so_pack_header_Zi.Rows[i].Cells["send_no"].Value.ToString().Trim() +
                                            "', address ='" + i_address +
                                            "',net_weight = " + net_weight +
                                             ",box_size ='" + dgv_so_pack_header_Zi.Rows[i].Cells["box_size"].Value.ToString().Trim() +
                                             "', carrier_postalcode='" + dgv_so_pack_header_Zi.Rows[i].Cells["box_number"].Value.ToString().Trim() +
                                             "', carrier_phone='" + dgv_so_pack_header_Zi.Rows[i].Cells["repack_type"].Value.ToString().Trim() +
                                             "', memo='" + dgv_so_pack_header_Zi.Rows[i].Cells["memo"].Value.ToString().Trim() +
                                            "' where status ='0' and box_no = '" + i_ds_so_pack_header_s.Tables[0].Rows[i]["box_no"].ToString().Trim() + "'";
                                i_sql_so_pack_header += " and so_no in (select so_no from " + main.g_space + ".so_ship_wavedetail where inv_org='" + main.g_org + "' and consignor='" + dgv_so_pack_header_Zi.Rows[i].Cells["so_pack_header_consignor"].Value.ToString().Trim() + "' and wave_no='" + wave_no + "') ";
                                string i_sql_so_pack_detail = " update " + main.g_space + ".so_pack_detail set  main_dist ='C',update_time =sysdate,updater='" + main.g_user + "', box_no='" + dgv_so_pack_header_Zi.Rows[i].Cells["so_pack_header_box_no"].Value.ToString().Trim() +
                                             "' where box_no = '" + i_ds_so_pack_header_s.Tables[0].Rows[i]["box_no"].ToString().Trim() + "'";
                                i_sql_so_pack_detail += " and so_no in (select so_no from " + main.g_space + ".so_ship_wavedetail where inv_org='" + main.g_org + "' and consignor='" + dgv_so_pack_header_Zi.Rows[i].Cells["so_pack_header_consignor"].Value.ToString().Trim() + "' and wave_no='" + wave_no + "') ";
                                //string str_sql = "update " + main.g_space + ".so_ship_header set  main_dist ='C',update_time =sysdate,updater='" + main.g_username + "', address ='" + dgv_so_pack_header_Zi.Rows[i].Cells["address"].Value.ToString().Trim().Substring(4, dgv_so_pack_header_Zi.Rows[i].Cells["address"].Value.ToString().Trim().Length-4) + "' where inv_org='" + main.g_org +
                                //                "' and consignor ='" + dgv_so_pack_header_Zi.Rows[i].Cells["so_pack_header_consignor"].Value.ToString().Trim() + "' and so_no ='" + dgv_so_pack_header_Zi.Rows[i].Cells["so_pack_header_so_no"].Value.ToString().Trim() + "'";

                                main.dbservice.oracle_exec_commandsql(i_sql_so_pack_header);
                                main.dbservice.oracle_exec_commandsql(i_sql_so_pack_detail);
                                //main.dbservice.oracle_exec_commandsql(str_sql);
                            }
                        }
                        catch (Exception ee)
                        {
                            MessageBox.Show(ee.Message.ToString());
                            return;
                        }
                }
            }
            i_close = 1;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void dgv_so_pack_header_Zi_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dgv_so_pack_header_Zi.Columns[dgv_so_pack_header_Zi.CurrentCell.ColumnIndex].Name.ToString().Trim() == "carrier")
            {
                if (dgv_so_pack_header_Zi.IsCurrentCellDirty)
                {
                    dgv_so_pack_header_Zi.CommitEdit(DataGridViewDataErrorContexts.Commit);
                }
            }

        }

        private void dgv_so_pack_header_Zi_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_so_pack_header_Zi.CurrentCell != null)
            {
                //if (dgv_so_pack_header_Zi.CurrentCell.ColumnIndex == 8)
                //{
                //    string s_sql = "select business_address,postalcode,telephone from " + main.g_space + ".mst_customer where consignor_flag='1' and address4='1' and inv_org='" + main.g_org + "' and consignor_name='" + dgv_so_pack_header_Zi.CurrentRow.Cells["carrier"].Value.ToString() + "'";
                //    DataSet ds = new DataSet();
                //    try
                //    {
                //        if (main.dbtype == "SQL")
                //        {
                //            ds = main.dbservice.Sql_exec_selectsql(s_sql);
                //        }
                //        else if (main.dbtype == "ORACLE")
                //        {
                //            ds = main.dbservice.oracle_exec_selectsql(s_sql);
                //        }
                //        if (ds.Tables[0].Rows.Count > 0)
                //        {
                //            dgv_so_pack_header_Zi.CurrentRow.Cells["carrier_address"].Value = ds.Tables[0].Rows[0]["business_address"].ToString();
                //            dgv_so_pack_header_Zi.CurrentRow.Cells["carrier_postalcode"].Value = ds.Tables[0].Rows[0]["postalcode"].ToString();
                //            dgv_so_pack_header_Zi.CurrentRow.Cells["carrier_phone"].Value = ds.Tables[0].Rows[0]["telephone"].ToString();
                //        }
                //        else
                //        {
                //            dgv_so_pack_header_Zi.CurrentRow.Cells["carrier_address"].Value = "";
                //            dgv_so_pack_header_Zi.CurrentRow.Cells["carrier_postalcode"].Value = "";
                //            dgv_so_pack_header_Zi.CurrentRow.Cells["carrier_phone"].Value = "";
                //        }

                //    }
                //    catch (System.Exception ee)
                //    {
                //        MessageBox.Show(main.g_class.comm_getchangestring("Fail to get bill's Information", main.g_info_dataset) + "!" + ee.ToString());
                //        return;
                //    }
                //}
                // //判断箱表是否重复
                //#region 判断箱表是否重复

                //if (dgv_so_pack_header_Zi.CurrentCell.ColumnIndex == 3)
                //{
                //    box_no_s = dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_box_no"].Value.ToString().Trim();
                //    int i = dgv_so_pack_header_Zi.Rows.Count - 1;
                //        string str_sql = "select content1 from " + main.g_space + ".sys_dictionary  where inv_org='" + main.g_org +
                //            "' and dictionary_name ='spec'" +
                //            " and dictionary_code = '" + box_no_s + "'";
                //        string str_sql2 = "select count(*) count from " + main.g_space + ".so_pack_header  where so_no ='" + dgv_so_pack_header_Zi.Rows[e.RowIndex].Cells["so_pack_header_so_no"].Value.ToString().Trim() + "' and send_no ='" + dgv_so_pack_header_Zi.Rows[e.RowIndex].Cells["send_no"].Value.ToString().Trim() + "'";

                //        try
                //        {
                //            if (main.dbtype == "SQL")
                //            {
                //                ds_temp = main.dbservice.Sql_exec_selectsql(str_sql);
                //                ds_temp_2 = main.dbservice.Sql_exec_selectsql(str_sql2);
                //            }
                //            else if (main.dbtype == "ORACLE")
                //            {
                //                ds_temp = main.dbservice.oracle_exec_selectsql(str_sql);
                //                ds_temp_2 = main.dbservice.oracle_exec_selectsql(str_sql2);
                //            }
                //            if (ds_temp.Tables[0].Rows.Count < 1)
                //            {
                //                MessageBox.Show(main.g_class.comm_getchangestring("箱号不存在", main.g_info_dataset) + "!");
                //                int rows = this.dgv_so_pack_header_Zi.CurrentRow.Index;
                //                dgv_so_pack_header_Zi.CurrentCell = dgv_so_pack_header_Zi["so_pack_header_box_no", rows];
                //                dgv_so_pack_header_Zi.BeginEdit(true);
                //                return;
                //            }
                //            dgv_so_pack_header_Zi.CurrentRow.Cells["spec"].Value = ds_temp.Tables[0].Rows[0]["content1"].ToString().Trim();
                //            int count = Convert.ToInt32(ds_temp_2.Tables[0].Rows[0]["count"].ToString().Trim()) + 1;
                //            string no;
                //            if (count < 10)
                //            {
                //                no = "00" + count.ToString();
                //            }
                //            else if (count < 100)
                //            {
                //                no = "00" + count.ToString();
                //            }
                //            else
                //            {
                //                no = count.ToString();
                //            }
                //            if (dgv_so_pack_header_Zi.CurrentRow.Cells["send_no"].Value.ToString().Trim() != "")
                //                dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_box_no"].Value = dgv_so_pack_header_Zi.CurrentRow.Cells["send_no"].Value.ToString().Trim() + "-" + no;
                //            else
                //                MessageBox.Show(main.g_class.comm_getchangestring("请输入运单号", main.g_info_dataset) + "!");
                //        }
                //        catch (Exception ex)
                //        {
                //            MessageBox.Show(ex.Message.ToString());
                //            return;
                //        }
                //    }
                //#endregion

            }
        }

        private void btn_subcancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void dgv_so_pack_header_Zi_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_so_pack_header_Zi.Columns[e.ColumnIndex].Name.Trim() == "so_pack_header_box_no")
            {
                box_no_s = dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_box_no"].Value.ToString().Trim();
                int i = dgv_so_pack_header_Zi.Rows.Count - 1;
                string str_sql = "select ltrim(rtrim(case use_flag when '1' then content1 when '2' then content2 when '3' then convert(char,number1) else convert(char,number2) end)) content1 from " + main.g_space + ".sys_dictionary  where inv_org='" + main.g_org +
                    "' and dictionary_name ='spec'" +
                    " and dictionary_code = '" + dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_box_no"].Value.ToString().Trim() + "'";
                string str_sql2 = "select count(*) count from " + main.g_space + ".so_pack_header  where status ='0' and so_no ='" + dgv_so_pack_header_Zi.Rows[e.RowIndex].Cells["so_pack_header_so_no"].Value.ToString().Trim() + "' and send_no ='" + dgv_so_pack_header_Zi.Rows[e.RowIndex].Cells["send_no"].Value.ToString().Trim() + "'";
                try
                {
                    if (main.dbtype == "SQL")
                    {
                        ds_temp = main.dbservice.Sql_exec_selectsql(str_sql);
                        ds_temp_2 = main.dbservice.Sql_exec_selectsql(str_sql2);
                    }
                    else if (main.dbtype == "ORACLE")
                    {
                        ds_temp = main.dbservice.oracle_exec_selectsql(str_sql);
                        ds_temp_2 = main.dbservice.oracle_exec_selectsql(str_sql2);
                    }
                    if (ds_temp.Tables[0].Rows.Count < 1)
                    {
                        MessageBox.Show(main.g_class.comm_getchangestring("箱号不存在", main.g_info_dataset) + "!");
                        int rows = this.dgv_so_pack_header_Zi.CurrentRow.Index;
                        dgv_so_pack_header_Zi.CurrentCell = dgv_so_pack_header_Zi["so_pack_header_box_no", rows];
                        dgv_so_pack_header_Zi.BeginEdit(true);
                        return;
                    }
                    dgv_so_pack_header_Zi.CurrentRow.Cells["box_size"].Value = ds_temp.Tables[0].Rows[0]["content1"].ToString().Trim();
                    //if (dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_box_no"].Value.ToString().Trim() != "99999")
                    //    dgv_so_pack_header_Zi.CurrentRow.Cells["spec"].Value = "纸箱";
                    //else
                    //    dgv_so_pack_header_Zi.CurrentRow.Cells["spec"].Value = "";
                    int count = Convert.ToInt32(ds_temp_2.Tables[0].Rows[0]["count"].ToString().Trim()) + 1;
                    string no;
                    if (count < 10)
                    {
                        no = "00" + count.ToString();
                    }
                    else if (count < 100)
                    {
                        no = "0" + count.ToString();
                    }
                    else
                    {
                        no = count.ToString();
                    }
                    if (dgv_so_pack_header_Zi.CurrentRow.Cells["send_no"].Value.ToString().Trim() != "")
                        dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_box_no"].Value = dgv_so_pack_header_Zi.CurrentRow.Cells["send_no"].Value.ToString().Trim() + "-" + no;
                    //else
                    //    MessageBox.Show(main.g_class.comm_getchangestring("请输入运单号", main.g_info_dataset) + "!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                    return;
                }
            }
            if (dgv_so_pack_header_Zi.Columns[e.ColumnIndex].Name.Trim() == "send_no")
            {
                //DataSet ds_check = new DataSet();
                //string check_wave = "select distinct wave_no from wms.so_pick_detail  where so_no in (select so_no from wms.so_pack_header where send_no ='" + dgv_so_pack_header_Zi.Rows[e.RowIndex].Cells["send_no"].Value.ToString().Trim() + "') ";
                
                //if (main.dbtype == "SQL")
                //{
                //    ds_check = main.dbservice.Sql_exec_selectsql(check_wave);
                //}
                //else if (main.dbtype == "ORACLE")
                //{
                //    ds_check = main.dbservice.oracle_exec_selectsql(check_wave);
                //}
                //int check =0;
                //if (ds_check.Tables[0].Rows.Count > 0)
                //{
                //    for (int i = 0; i < ds_check.Tables[0].Rows.Count; i++)
                //    {
                //        if (wave_no.ToString().Trim() != ds_check.Tables[0].Rows[i]["wave_no"].ToString().Trim())
                //            check++;
                //    }
                //}
                //if (check > 0)
                //{
                //    MessageBox.Show("该运单已被其他箱子占用！");
                //    int rows = this.dgv_so_pack_header_Zi.CurrentRow.Index;
                //    dgv_so_pack_header_Zi.CurrentCell = dgv_so_pack_header_Zi["send_no", rows];
                //    dgv_so_pack_header_Zi.BeginEdit(true);
                //    return;
                //}


                string str_sql2 = "select count(*) count from " + main.g_space + ".so_pack_header  where status ='0' and so_no ='" + dgv_so_pack_header_Zi.Rows[e.RowIndex].Cells["so_pack_header_so_no"].Value.ToString().Trim() + "' and send_no ='" + dgv_so_pack_header_Zi.Rows[e.RowIndex].Cells["send_no"].Value.ToString().Trim() + "'";
                try
                {
                    if (main.dbtype == "SQL")
                    {
                        ds_temp_2 = main.dbservice.Sql_exec_selectsql(str_sql2);
                    }
                    else if (main.dbtype == "ORACLE")
                    {
                        ds_temp_2 = main.dbservice.oracle_exec_selectsql(str_sql2);
                    }
                    if (ds_temp.Tables.Count > 0)
                    {
                        if (dgv_so_pack_header_Zi.CurrentRow.Cells["box_size"].Value.ToString().Trim() == "")
                        {
                            dgv_so_pack_header_Zi.CurrentRow.Cells["box_size"].Value = ds_temp.Tables[0].Rows[0]["content1"].ToString().Trim();
                        }
                        //if (dgv_so_pack_header_Zi.CurrentRow.Cells["spec"].Value.ToString().Trim() =="")
                        //{
                        //    if (dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_box_no"].Value.ToString().Trim() != "99999")
                        //        dgv_so_pack_header_Zi.CurrentRow.Cells["spec"].Value = "纸箱";
                        //    else
                        //        dgv_so_pack_header_Zi.CurrentRow.Cells["spec"].Value = "";
                        //}
                        int count = Convert.ToInt32(ds_temp_2.Tables[0].Rows[0]["count"].ToString().Trim()) + 1;
                        string no;
                        if (count < 10)
                        {
                            no = "00" + count.ToString();
                        }
                        else if (count < 100)
                        {
                            no = "00" + count.ToString();
                        }
                        else
                        {
                            no = count.ToString();
                        }
                        dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_box_no"].Value = dgv_so_pack_header_Zi.CurrentRow.Cells["send_no"].Value.ToString().Trim() + "-" + no;
                    }
                    else
                    {
                        dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_box_no"].Value = dgv_so_pack_header_Zi.CurrentRow.Cells["send_no"].Value.ToString().Trim() + "-001";
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                    return;
                }
                
            }
            
        }



        private void dgv_so_pack_header_Zi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 8)
            {
                _Rectangle = dgv_so_pack_header_Zi.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); //得到所在单元格位置和大小
                cmb_address.Size = new Size(_Rectangle.Width, _Rectangle.Height); //把单元格大小赋给时间控件

                cmb_address.Location = new Point(_Rectangle.X, _Rectangle.Y); //把单元格位置赋给时间控件

                cmb_address.Visible = true;  //可以显示控件了
                ds_temp_2 = new DataSet();
                string str_sql = "select case rtrim(ltrim(address)) when '' then '' else '单据 /'+ rtrim(ltrim(address)) end address from " + main.g_space + ".so_ship_header where inv_org = '" + main.g_org + "' and consignor ='" + dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_consignor"].Value.ToString().Trim() + "' and so_no ='" + dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_so_no"].Value.ToString().Trim() + "' union " +
                                 "select case rtrim(ltrim(a.business_address)) when '' then '' else '快递 /'+ rtrim(ltrim(a.business_address)) end  address from " + main.g_space + ".mst_customer a," + main.g_space + ".so_ship_header b where a.inv_org = b.inv_org and a.consignor =b.customer_code and b.inv_org = '" + main.g_org + "' and b.consignor ='" + dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_consignor"].Value.ToString().Trim() + "' and b.so_no ='" + dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_so_no"].Value.ToString().Trim() + "' union " +
                                 "select case rtrim(ltrim(a.register_address)) when '' then '' else '公路1/'+ rtrim(ltrim(a.register_address)) end  address from " + main.g_space + ".mst_customer a," + main.g_space + ".so_ship_header b where a.inv_org = b.inv_org and a.consignor =b.customer_code and b.inv_org = '" + main.g_org + "' and b.consignor ='" + dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_consignor"].Value.ToString().Trim() + "' and b.so_no ='" + dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_so_no"].Value.ToString().Trim() + "' union " +
                                 "select case rtrim(ltrim(a.address3)) when '' then '' else '公路2/'+ rtrim(ltrim(a.address3)) end address from " + main.g_space + ".mst_customer  a," + main.g_space + ".so_ship_header b where a.inv_org = b.inv_org and a.consignor =b.customer_code and b.inv_org = '" + main.g_org + "' and b.consignor ='" + dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_consignor"].Value.ToString().Trim() + "' and b.so_no ='" + dgv_so_pack_header_Zi.CurrentRow.Cells["so_pack_header_so_no"].Value.ToString().Trim() + "'";
                                 
                if (main.dbtype == "SQL")
                {
                    ds_temp_2 = main.dbservice.Sql_exec_selectsql(str_sql);
                }
                else if (main.dbtype == "ORACLE")
                {
                    ds_temp_2 = main.dbservice.oracle_exec_selectsql(str_sql);
                }
                //DataGridViewComboBoxColumn dgvComboBoxColumn = dgv_so_pack_header_Zi.Columns[9] as DataGridViewComboBoxColumn;
                cmb_address.ValueMember = "address";
                cmb_address.DisplayMember = "address";
                cmb_address.DataSource = ds_temp_2.Tables[0];
                cmb_address.SelectedText = dgv_so_pack_header_Zi.CurrentRow.Cells["address"].Value.ToString().Trim();
            }
            for (int i = 0; i < dgv_so_pack_header_Zi.Rows.Count; i++)
            {
                if (i == e.RowIndex)
                {
                    dgv_so_pack_header_Zi.Rows[i].DefaultCellStyle.BackColor = Color.SteelBlue;
                    dgv_so_pack_header_Zi.Rows[i].DefaultCellStyle.ForeColor = Color.White;
                }
                else
                {
                    dgv_so_pack_header_Zi.Rows[i].DefaultCellStyle.BackColor = Color.White;
                    dgv_so_pack_header_Zi.Rows[i].DefaultCellStyle.ForeColor = Color.Black;
                }
            }
            
        }

        private void dgv_so_pack_header_Zi_Scroll(object sender, ScrollEventArgs e)
        {
            cmb_address.Visible = false;
        }

        private void dgv_so_pack_header_Zi_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        //private void cmb_address_Change(object sender, EventArgs e)
        //{
        //    //dgv_so_pack_header_Zi.CurrentRow.Cells["address"].Value = cmb_address.Text.ToString();  //时间控件选择时间时，就把时间赋给所在的单元格
        //    //cmb_address.Visible = false;
        //}
    }
}